package com.bootdo.lgs.orderbind.service.impl;

import com.bootdo.common.utils.AnnoExportExcel;
import com.bootdo.lgs.orderbind.dao.OrderBindMapper;
import com.bootdo.lgs.orderbind.model.LgsOrderBind;
import com.bootdo.lgs.orderbind.model.OrderBindQuery;
import com.bootdo.lgs.orderbind.model.OrderBindUpdate;
import com.bootdo.lgs.orderbind.model.OrderBindUpload;
import com.bootdo.lgs.orderbind.service.OrderBindService;
import com.bootdo.system.domain.UserDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class OrderBindServiceImpl implements OrderBindService {

    private static final Logger logger = LoggerFactory.getLogger(OrderBindServiceImpl.class);

    @Autowired
    OrderBindMapper orderBindMapper;

    @Override
    public List getList(OrderBindQuery condition) {
        return orderBindMapper.getList(condition);
    }

    @Override
    public boolean batchUpdateLgsNumber(List<OrderBindUpdate> condition, UserDO loginUser) {
        Date date = new Date();
        try {
            for (OrderBindUpdate orderBindUpdate : condition) {
                orderBindUpdate.setBind_date(date);
                orderBindUpdate.setBind_user_id(loginUser.getUserId());
            }
            orderBindMapper.batchUpdateLgsNumber(condition);
        } catch (Exception e) {
            logger.warn("批量绑定单号失败:"+condition,e);
        }
        return true;
    }

    @Override
    public boolean updateLgsNumber(OrderBindUpdate condition, UserDO loginUser) {
        condition.setBind_date(new Date());
        condition.setBind_user_id(loginUser.getUserId());
        return orderBindMapper.updateLgsNumber(condition) > 0;//不修改运单回填时间
    }

    @Override
    public boolean isTrackNumberRepeatWhenBind(OrderBindUpdate condition) {
        LgsOrderBind order = orderBindMapper.getById(condition.getId());
        if (order == null) {
            throw new IllegalArgumentException("Invalid order id: " + condition.getId());
        }
        condition.setLgs_company_cd(order.getLgs_company_cd());
        return orderBindMapper.cntLgsOrderWhenUpdate(condition)> 0;
    }

    @Override
    public boolean isTrackNumberRepeatWhenUpdate(OrderBindUpdate condition) {
        LgsOrderBind order = orderBindMapper.getById(condition.getId());
        if (order == null) {
            throw new IllegalArgumentException("Invalid order id: " + condition.getId());
        }
        condition.setLgs_company_cd(order.getLgs_company_cd());
        return orderBindMapper.cntLgsOrderWhenUpdate(condition)> 0;
    }

    @Override
    public boolean checkTrackNumber(Long orderId, String trackNumber) {
        LgsOrderBind order = orderBindMapper.getById(orderId);
        if (order == null) {
            throw new IllegalArgumentException("Invalid order id: " + orderId);
        }

        OrderBindQuery param = new OrderBindQuery();
        param.setLgsCompanyCd(order.getLgs_company_cd());
        return orderBindMapper.getList(param).stream()
                .filter(item -> !item.getId().equals(orderId) && trackNumber.equals(item.getLgs_track_number()))
                .count() == 0;
    }

    @Override
    public void exportMng(OrderBindQuery condition, HttpServletResponse response) {
        try {
            new AnnoExportExcel("物流对账单",LgsOrderBind.class, 2).setDataList(orderBindMapper.getList(condition)).write(response);
        } catch (IOException e) {
            e.printStackTrace();
            //log.warn("调度令管理列表-导出excel失败，condition:"+condition,response);
        }
    }

    @Override
    public boolean rollback(Map condition) {
        return orderBindMapper.rollback(condition) > 0;
    }

    @Override
    public boolean updateReceiptPic(OrderBindUpload condition) {
        return orderBindMapper.updateReceiptPic(condition)>0;
    }

    @Override
    public boolean delReceipt(Long orderId) {
        //根据id删除记录
        //删除图片 待做
        //修改订单表回单图片为空
        return orderBindMapper.updateReceiptEmpty(orderId)>0;
    }
}
